package domain;

import gui.GUIController;
import domain.player.*;

/**
 * DomainController handles request between the logic and the GUI elements. It's
 * part of the glue between the backend and the frontend. hej
 */
public class DomainController {

    private GameManager gameManager;
    private GameGrid gameGrid;
    private GUIController guiController;

    public DomainController(GUIController guiController) {
        gameGrid = new GameGrid(this);
        this.guiController = guiController;
        gameManager = new GameManager(this, gameGrid);
    }

    /**
     * Quit game
     */
    public void quitGame() {
        gameManager = null;
        gameGrid = null;
        guiController = null;
    }

    /**
     * Adds a brick to the game grid
     */
    public void addDisc(int column) {
        if (gameGrid.addDisc(column) != -1) {
            guiController.addDisc(column);
        }
    }

    /**
     * Gets the next move for the player from the GameManager and returns its
     * column number
     */
    public int getNextMove() {
        return gameManager.getCurrentPlayer().getNextMove(gameGrid);
    }

    /**
     * Returns the game grid as an Disc matrix from GameGrid
     */
    public GameGrid getGameGrid() {
        return gameGrid;
    }

    /**
     * Returns a Player object corresponding to playerNum, by calling
     * GameManager
     */
    public Player getPlayer(int playerNum) {
        return gameManager.getPlayer(playerNum);
    }

    /*
     * Returns a Player object for the current player, by calling GameManager
     */
    public Player getCurrentPlayer() {
        return gameManager.getCurrentPlayer();
    }

    /*
     * Add players by calling the GameManager
     */
    public void addPlayers(Player player1, Player player2) {
        gameManager.addPlayers(player1, player2);
    }

    /**
     * Starts the game by calling the GameFrame
     */
    public void startGame() {
        gameManager.startGame();
    }

    /**
     * Resets the ongoing game
     */
    public void resetGameGrid() {
        gameGrid.resetGameGrid();
    }

    /**
     * Sets the current player's name by calling the GameFrame
     */
    public void setCurrentPlayerFrameName(Player currentPlayer) {
        guiController.setCurrentPlayerFrameName(currentPlayer);
    }

    /**
     * Show the winner dialog
     */
    public void showWinnerDialog(Player winner) {
        guiController.showWinnerDialog(winner);
    }

    /**
     * Show the tie dialog
     */
    public void showDrawnDialog() {
        guiController.showDrawnDialog();
    }

    /**
     * Presents the winner board
     */
    public void presentWinnerBoard() {
        guiController.presentWinnerBoard(gameGrid.getGameRules());
    }

    public void presentMoveWithAndWithout(int move, int nrOfMovesMM, int nrOfMovesMMABP) {
        guiController.presentMoveWithAndWithout(move, nrOfMovesMM, nrOfMovesMMABP);
    }

    public void setDebug(boolean debug) {
        gameManager.setDebug(debug);
    }

    public void setGameGrid(GameGrid gameGrid) {
        this.gameGrid = gameGrid;
        gameManager.setGameGrid(gameGrid);
    }

    public GUIController getGuiController() {
        return guiController;
    }
}
